<?php

##################################################
#
# Copyright (c) 2008 - 2009 ABSoft Company
# Written and Designed by Dang Tin Trung
#
# This file is part of ABSoft Framework
#
##################################################

if (!defined('EXPONENT')) exit('');

/**
 * Ajax Upload Control
 *
 * @author Dang Tin Trung
 * @copyright 2008 - 2009 ABSoft Company
 * @version 1.0
 * 
 * @package Subsystems
 * @subpackage Forms
 */

/**
 * Manually include the class file for formcontrol, for PHP4
 * (This does not adversely affect PHP5)
 */
require_once(BASE."subsystems/forms/controls/formcontrol.php");

/**
 * Ajax Upload Control
 *
 * @package Subsystems
 * @subpackage Forms
 */
class ajax_uploadcontrol extends formcontrol {
	function name() { return "Ajax Uploader"; }
	function isSimpleControl() { return true; }
	function getFieldDefinition() {
                return array(
			DB_FIELD_TYPE=>DB_DEF_STRING,
			DB_FIELD_LEN=>250,
		);
        }
	
	function ajax_uploadcontrol($load_js = true, $id_name = "fileupload", $collection_id = 0) {
		$this->load_js = $load_js;
		$this->id_name = $id_name;
		$this->collection_id = $collection_id;
		$this->session_id = session_id();
	}
	
	function onRegister(&$form) {
		$form->enctype = "multipart/form-data";
	}

	function controlToHTML($name) {
		if ($this->load_js)
			$html = "<script type='text/javascript' src='" .   THEME_RELATIVE . "upload/jquery.uploadify.js'></script><link rel='stylesheet' href='" . THEME_RELATIVE . "upload/uploadify.css' type='text/css' />";
		else
			$html="";
		// init script
		$html.="<script language='Javascript'>
				function efm_pickedFile(id, filepath)
				{
					var upload_control = document.getElementById('".$name."');
					//upload_control.value=id;
					//upload_control = document.getElementById('".$this->id_name."_divname');
					//upload_control.innerHTML = 'Selected file on ' + filepath;
				}
				$(document).ready(function() {
				$('#".$this->id_name."').fileUpload({
				'uploader': '" . THEME_RELATIVE . "upload/uploader.swf',
				'cancelImg': '".THEME_RELATIVE. "upload/cancel.png',
				'script': '".PATH_RELATIVE."modules/filemanagermodule/actions/upload_standalone.php',
				'multi': false,
				'buttonText': 'Browse',";
				$html.="'scriptData': {'collection_id':'".$this->collection_id."', 
				'save_session': '".$this->session_id."'},
				'displayData': 'speed',
				onComplete: function (evt, queueID, fileObj, response, data) {
					var temp = response.indexOf(' ');
		      var temp_text = response.substr(temp+1);
		      temp = response.substr(0,temp);
		      var upload_control= document.getElementById('".$name."');
					upload_control.value=temp;
					upload_control = document.getElementById('".$this->id_name."_divname');
					upload_control.innerHTML = 'File Uploaded with ID = ' + temp_text;
					return true;
				}
				});});</script>";
		// build fake file ID. With value = -1, we understand that file has not uploaded
		$html.="<input id='".$name."' type=hidden name='".$name."' value='-1'>";
		$html.="<div id='".$this->id_name."'>
				<input type='file' class='filebtn' name='fileupload' enctype='multipart/form-data' size='35' value='1'>
You have a problem with your javascript</div>
				<div>
					<b>Status:&nbsp;</b>
					<span id='".$this->id_name."_divname'>No File Selected or Uploaded</span>
				</div>
				<a id='".$this->id_name."_a' href=\"javascript:$('#".$this->id_name."').fileUploadStart()\">Upload now</a>&nbsp;&nbsp;
				<a id='".$this->id_name."_s'  href='#' onclick=\"javascript:window.open('".PATH_RELATIVE."modules/filemanagermodule/actions/picker.php?id=0');\">Select Uploaded</a></div>";
		return $html;
	}

	function form($object) {
                if (!defined("SYS_FORMS")) require_once(BASE."subsystems/forms.php");
                exponent_forms_initialize();

                $form = new form();
                if (!isset($object->identifier)) {
                        $object->identifier = "";
                        $object->caption = "";
                        $object->default = "";
                }
                $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');

                $form->register("identifier",$i18n['identifier'],new textcontrol($object->identifier));
                $form->register("caption",$i18n['caption'], new textcontrol($object->caption));
                $form->register("default",$i18n['default'], new textcontrol($object->default));
                $form->register("submit","",new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));
                return $form;
        }

	function update($values, $object) {
                if ($object == null) $object = new uploadcontrol();
                if ($values['identifier'] == "") {
                        $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');
                        $post = $_POST;
                        $post['_formError'] = $i18n['id_req'];
                        exponent_sessions_set("last_POST",$post);
                        return null;
                }
                $object->identifier = $values['identifier'];
                $object->caption = $values['caption'];
                $object->default = $values['default'];
                return $object;
        }

	function parseData($original_name,$formvalues) {
		$file = $formvalues[$original_name];
		return '<a href="'.URL_FULL.$file.'">'.basename($file).'</a>';
	}
}

?>
